--################# HEADER #################
AddCSLuaFile("cl_init.lua");
AddCSLuaFile("shared.lua");
include("shared.lua");
--################# SENT CODE ###############

--################# Init @JDM12989
function ENT:Initialize()
	self.Entity:SetModel("models/zup/zpm/zpm.mdl");
	self.Entity:SetMaterial("zup/zpm/zpm.vmt");
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);
	self.MaxEnergy = StarGate.CFG:Get("ZPM_Mk2","MaxEnergy",10000000000);
	self.MacStorage = StarGate.CFG:Get("ZPM_Mk2","MaxStorage",50000000);
	self.enabled = false;
	self:AddResource("ZPE",self.MaxEnergy); --Zero Point energy @Anorr
	self:SupplyResource("ZPE",self.MaxEnergy);
	self:AddResource("energy",self.MaxStorage); -- Maximum energy to store in a ZPM
	self.Entity:CallOnRemove("RmMK2Count", self.DecCount);
	if(StargateExtras) then
		self:CreateWireInputs("Disable","Overload");
	else
		self:CreateWireInputs("Disable");
	end
	local phys = self.Entity:GetPhysicsObject();
	if(phys:IsValid()) then
		phys:Wake();
		phys:SetMass(2);
	end
end

--################# Spawn the SENT @JDM12989
function ENT:SpawnFunction(p,t)
	if(not t.Hit) then return end;
	local e = ents.Create("Zero_Point_Module_Mk2");
	e:SetPos(t.HitPos+Vector(0,0,10));
	e:Spawn();
	return e;
end

function ENT.DecCount()
	MckaysPack.Ents.ZeroPointModuleMK2.Count = MckaysPack.Ents.ZeroPointModuleMK2.Count-1
end

--################# Adds the annoying overlay speechbubble to this SENT @JDM12989
function ENT:ShowOutput(v,force)
	local add = "(Off)";
	if(self.enabled) then add = "(On)" end;
	self:SetOverlayText("Zero Point Module Mk2 "..add.."\n"..v.."%");
end

--################# Think @JDM12989
function ENT:Think()
	if(self.depleted or not self.HasResourceDistribution) then return end;
	local energy = self:GetResource("energy");
	local ZPE = self:GetResource("ZPE");
	local my_capacity = self:GetUnitCapacity("energy");
	local nw_capacity = self:GetNetworkCapacity("energy");
	local percent = (ZPE/self.MaxEnergy)*100;
	if(not self.disabled) then
		self.resfix = false;
		if(my_capacity ~= nw_capacity) then -- We are connected to a network - Enable ZPM
			if(not self.enabled) then
				self.Entity:SetMaterial("Zup/ZPM/zpm_on.vmt");
				self.enabled = true;
			end
		else
			if(self.enabled) then
				self.Entity:SetMaterial("Zup/ZPM/zpm.vmt");
				self.enabled = false;
			end
		end
	else
		self.Entity:SetMaterial("Zup/ZPM/zpm.vmt");
		self.enabled = false;
		if(not self.resfix) then
			self:ConsumeResource("energy",self.MaxStorage);   -- This is to remove the excess energy stored in an energy cell, if ZPM is disabled.
			self:SupplyResource("ZPE",self.MaxStorage);  -- This is adding the removed energy back into the ZPM
		end
		self.resfix = true;
	end
	-- No ZeroPointEnergy available anymore - We are depleted!
	if(ZPE == 0) then
		self.Entity:SetMaterial("Zup/ZPM/zpm_depleted.vmt");
		self.depleted = true;
		self.enabled = false;
		self:SetOverlayText("Zero Point Module Mk2\nDepleted");
	end
	-- Energy conversion when availeble storage @Anorr,aVoN
	if(self.enabled and not self.disabled and energy < nw_capacity) then
			local rate = (my_capacity+nw_capacity)/2; -- Two passes until it filled the full network
			rate = math.Clamp(rate,0,ZPE);
			rate = math.Clamp(rate,0,nw_capacity-energy);
			self:SupplyResource("energy",rate);
			self:ConsumeResource("ZPE",rate);
	end
	if(self.depleted and Dev_Unlink_All) then
		Dev_Unlink_All(self.Entity);
		self:SetWire("Active",-1);
	else
		self:ShowOutput(percent);
		self:SetWire("Active",self.enabled);
		self:SetWire("Energy",math.floor(energy));
		self:SetWire("Zero Point Energy",math.floor(ZPE));
		self:SetWire("ZPM %",percent);
	end
	self.Entity:NextThink(CurTime()+0.5);
	return true;
end

--Using aVoN's Wire class thingy ??
function ENT:TriggerInput(name,value)
	if(name == "Disable") then
		if (value == 1) then
			self:SetWire("Disabled",1);
			self.disabled = true;
		else
			self:SetWire("Disabled",0);
			self.disabled = false;
		end
	end
	if(name == "Overload") then
		if(value == 1 and not self.depleted) then
			self.disabled = true;

			local shake = ents.Create("env_shake")
			shake:SetKeyValue("amplitude", 2.5)
			shake:SetKeyValue("duration", 7)
			shake:SetKeyValue("radius", 163.84*(StarGate.CFG:Get("ZPM_Mk2","ExplosionRadius",6)*5)) 
			shake:SetKeyValue("frequency", 2.3*3)
			shake:SetPos(self.Entity:GetPos())
			shake:Spawn()
			shake:Fire("StartShake","","0.6")
			shake:Fire("kill","","8")
			
			timer.Create( "Boom_Time"..self.Entity:EntIndex(), 5, 0, function()
				if not self.Entity:IsValid() then return end;
				local nuke = ents.Create("gate_nuke")
				nuke:Setup(self:GetPos(), StarGate.CFG:Get("ZPM_Mk2","ExplosionRadius",6))	
				nuke:Spawn()
				nuke:Activate()
				timer.Destroy("Boom_Time"..self.Entity:EntIndex());
				self.Remove(self.Entity);
			end );
		else
			timer.Destroy("Boom_Time"..self.Entity:EntIndex());
			self.disabled = false;
		end
	end
end

function ENT:Draw()
	self.Entity:DrawModel()
end
